package replica;

import common.CommonTools;
import common.Marshall;

public class ReplicaServices {
	
	private String role;
	public ReplicaServices(String role){
		this.role = role;
	}
	
	public String createPlayer(String UDPRequest, String geolocation,PlayerClient playerClient ) {
		String result = null;
		
		if(
				(Marshall.getValue("UserName", UDPRequest).startsWith("Clinton100"))&& role.equals("replica_1")
				|| (Marshall.getValue("UserName", UDPRequest).startsWith("Clinton200"))&& role.equals("replica_2")
				|| (Marshall.getValue("UserName", UDPRequest).startsWith("Clinton300"))&& role.equals("replica_3")
				|| (Marshall.getValue("UserName", UDPRequest).startsWith("Clinton400"))&& role.equals("replica_4")
				|| (Marshall.getValue("UserName", UDPRequest).startsWith("Clinton500"))&& role.equals("replica_5")
				|| (Marshall.getValue("UserName", UDPRequest).startsWith("Clinton600"))&& role.equals("replica_6")
				|| (Marshall.getValue("UserName", UDPRequest).startsWith("Clinton700"))&& role.equals("replica_7")
				|| (Marshall.getValue("UserName", UDPRequest).startsWith("Clinton800"))&& role.equals("replica_8")
				|| (Marshall.getValue("UserName", UDPRequest).startsWith("Clinton900"))&& role.equals("replica_9")
				|| (Marshall.getValue("UserName", UDPRequest).startsWith("Clinton1000"))&& role.equals("replica_10")
		  ){
			return "false";
			
		}
		result = playerClient.createPlayer( Marshall.getValue("FirstName", UDPRequest),
											Marshall.getValue("LastName", UDPRequest) , 
											Integer.parseInt(Marshall.getValue("Age", UDPRequest)), 
											Marshall.getValue("UserName", UDPRequest) , 
											Marshall.getValue("Password", UDPRequest) , 
											Marshall.getValue("PlayerIpAddress", UDPRequest) );
		
		return result;
	}
	public String signInPlayer(String UDPRequest, String geolocation,PlayerClient playerClient ) {
		String result = null;
		
		result = playerClient.signIn(	Marshall.getValue("UserName", UDPRequest) , 
										Marshall.getValue("Password", UDPRequest) , 
										Marshall.getValue("PlayerIpAddress", UDPRequest) );
		
		return result;
	}
	public String signOutPlayer(String UDPRequest, String geolocation,PlayerClient playerClient ) {
		String result = null;
		
		result = playerClient.signOut( Marshall.getValue("UserName", UDPRequest) , 
									   Marshall.getValue("PlayerIpAddress", UDPRequest) );
		
		return result;
	}
	public String transferPlayer(String UDPRequest, String geolocation,PlayerClient playerClient ) {
		String result = null;
		
		result = playerClient.transferAccount(Marshall.getValue("UserName", UDPRequest) , 
											Marshall.getValue("Password", UDPRequest) , 
											Marshall.getValue("PlayerIpAddress", UDPRequest),
											Marshall.getValue("NewPlayerIpAddress", UDPRequest));
		
		return result;
	}
	
	public String getPlayerStatus(String UDPRequest, String geolocation, AdminClient adminClient ) {
		String result = null;
		
		result = adminClient.getPlayerStatus(Marshall.getValue("AdminName", UDPRequest) , 
											Marshall.getValue("AdminPassword", UDPRequest) , 
											Marshall.getValue("AdminIpAddress", UDPRequest));
		
		return result;
	}
	public String suspendAccount(String UDPRequest, String geolocation, AdminClient adminClient ) {
		String result = null;
		
		result = adminClient.suspendAccount(Marshall.getValue("AdminName", UDPRequest) , 
											Marshall.getValue("AdminPassword", UDPRequest) , 
											Marshall.getValue("AdminIpAddress", UDPRequest),
											Marshall.getValue("UserNameToSuspend", UDPRequest));
		
		return result;
	}
	public String halt(String UDPRequest, String geolocation, AdminClient adminClient){
		
		CommonTools
		.writeLog(
				role,
				role,
				"A UDP request to stop the "+ role +" server was received:"
				+ UDPRequest, "replica");
				
		
		String result = adminClient.haltLocalServer(geolocation);		
		
		return result;
	}
	

}
